<template>
  <div id="app">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">订单管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-col :span="8">
            <el-input
              placeholder="请输入内容"
              v-model="queryInfo.query"
              class="input-with-select"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getOrderList"
              ></el-button>
            </el-input>
          </el-col>
        </el-row>
      </div>
      <!-- 卡片主体区域 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="95px">
        </el-table-column>
        <el-table-column
          prop="pay_status"
          label="是否付款"
          v-slot="scope"
          width="95px"
        >
          <el-tag type="danger" v-if="scope.row.pay_status === '0'"
            >未付款</el-tag
          >
          <el-tag v-if="scope.row.pay_status === '1'">已付款</el-tag>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="95px">
        </el-table-column>
        <el-table-column
          prop="create_time"
          label="下单时间"
          v-slot="scope"
          width="195px"
        >
          {{ scope.row.create_time | timeFormat }}</el-table-column
        >
        <el-table-column label="操作" v-slot="scope" width="195px">
          <el-button
            type="primary"
            @click="editOrder(scope.row.order_id)"
            icon="el-icon-edit"
            size="mini"
          ></el-button>
          <el-button
            type="success"
            @click="locationOreder(scope.row.order_id)"
            icon="el-icon-location"
            size="mini"
          ></el-button>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 8, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 编辑订单对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="resetOreder"
    >
      <el-form
        :model="editOrderForm"
        :rules="editOrderRules"
        ref="editOrderRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            :options="cityData"
            :props="addressProps"
            v-model="editOrderForm.address1"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editOrderForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 查询物流信息对话框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="locationDialogVisible"
      width="50%"
    >
      <!-- 时间线组件 -->
      <el-timeline :reverse="true">
        <el-timeline-item
          v-for="(activity, index) in locationList"
          :key="index"
          :icon="activity.icon"
          :type="activity.type"
          :color="activity.color"
          :size="activity.size"
          :timestamp="activity.timestamp"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="locationDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="locationDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import cityData from './citydata.js'
export default {
  data() {
    return {
      // 查询参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      // table绑定订单数据
      orderList: [],
      editDialogVisible: false,
      editOrderForm: {
        address1: [],
        address2: ''
      },
      //属性名与属性值重名可直接简写cityData
      cityData,
      addressProps: {
        expandTrigger: 'hover',
        children: 'children'
      },
      editOrderRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 控制查询物流对话框的显示与隐藏
      locationDialogVisible: false,
      // 查询物流数据，组件的数据一般就是[{}]格式
      locationList: [{}]
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 获取订单数据
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取数据失败')
      }
      this.orderList = res.data.goods
      this.total = res.data.total
      console.log(this.orderList)
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 编辑订单表单
    editOrder(row) {
      this.editDialogVisible = true
    },
    resetOreder() {
      this.$refs.editOrderRef.resetFields()
    },
    //查询物流按钮,row的订单编号是假的，所以不用row
    async locationOreder() {
      const { data: res } = await this.$http.get(`/kuaidi/70365716896101`)
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流信息失败')
      }
      this.locationList = res.data
      // 给每一个节点自定义颜色
      if (this.locationList.length === 1) {
        this.locationList[0].type = 'danger'
      } else {
        this.locationList.forEach(item => {
          item.color = '#0bbd87'
        })
      }
      this.locationDialogVisible = true
      console.log(this.locationList)
    }
  }
}
</script>
<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
